package com.hualan.servlet.goodsServlet;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.JsonResult;
import com.hualan.bean.PanghuShop;
import com.hualan.bean.PanghuShopGoods;
import com.hualan.enums.ResponseEnum;
import com.hualan.service.GoodsService;
import com.hualan.service.ShopService;
import com.hualan.service.impl.GoodsServiceImpl;
import com.hualan.service.impl.ShopServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Date;
import java.util.Map;
import java.util.Objects;


@WebServlet("/goods/addShopGoods")
@SuppressWarnings("all")
public class GoodsAddServlet extends HttpServlet {
	
	private GoodsService goodsService;
	private ShopService shopService;
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			goodsService = new GoodsServiceImpl();
			shopService = new ShopServiceImpl();
			String filePath = (String) req.getSession().getAttribute("filePath");
			String line;
			StringBuilder stringBuilder = new StringBuilder();
			try (BufferedReader reader = req.getReader()) {
				while ((line = reader.readLine()) != null) {
					stringBuilder.append(line);
				}
			}
			;
			Map<String, Object> map = JSON.parseObject(stringBuilder.toString(), Map.class);
			PanghuShopGoods panghuShopGoods = new PanghuShopGoods();
			panghuShopGoods.setGoodsId(1);
			Field[] declaredFields = panghuShopGoods.getClass().getDeclaredFields();
			// TODO：反射封装属性
			for (Field declaredField : declaredFields) {
				if (map.get(declaredField.getName()) != null) {
					try {
						if (declaredField.getType() == BigDecimal.class){
							BigDecimal bigDecimal = new BigDecimal(String.valueOf(map.get(declaredField.getName())));
							declaredField.setAccessible(true);
							declaredField.set(panghuShopGoods,bigDecimal);
							continue;
						}
						declaredField.setAccessible(true);
						declaredField.set(panghuShopGoods, map.get(declaredField.getName()));
					} catch (IllegalAccessException e) {
						throw new RuntimeException(e);
					}
				}
			}
			// TODO:封装时间戳
			if (Objects.isNull(panghuShopGoods.getAddTime())){
				Date date = new Date();
				panghuShopGoods.setAddTime(date);
			}
			
			
			// TODO：查询商家继续封装属性
			PanghuShop shop = shopService.getShopById(panghuShopGoods.getShopId());
			if (Objects.nonNull(shop)){
				panghuShopGoods.setGoodsShopName(shop.getShopName());
				panghuShopGoods.setGoodsStatus(1);
			} else {
				String errorMesage = JSON.toJSONString(JSON.toJSON(JsonResult.error(ResponseEnum.INCORRECT_PARAMS, "商家为空")));
				resp.getWriter().write(errorMesage);
			}
			
			// TODO:调用service插入表中
			int i = 0;
			try {
				i = goodsService.addShop(panghuShopGoods);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
			if (i > 0){
				resp.getWriter().write(JSON.toJSONString(JsonResult.success(200,"添加成功")));
			} else {
				resp.getWriter().write(JSON.toJSONString(JsonResult.error(500,"添加失败")));
			}
		}catch (Exception e){
			e.printStackTrace();
			resp.getWriter().write(JSON.toJSONString(JsonResult.error(400,"系统异常")));
		}

	}
}
